<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>解析一个配置文件</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.parse-ini-file" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">parse_ini_file</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">parse_ini_file</span> &mdash; <span class="dc-title">解析一个配置文件</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.parse-ini-file-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>parse_ini_file</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$process_sections</code><span class="initializer"> = false</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$scanner_mode</code><span class="initializer"> = INI_SCANNER_NORMAL</span></span>
  ]] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>parse_ini_file()</strong></span> 载入一个由
   <code class="parameter">filename</code> 指定的 ini
   文件，并将其中的设置作为一个联合数组返回。
  </p>
  <p class="para">
   ini 文件的结构和 <var class="filename">php.ini</var> 的相似。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.parse-ini-file-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">filename</code></dt>

     <dd>

      <p class="para">
       要解析的 ini 文件的文件名。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">process_sections</code></dt>

     <dd>

      <p class="para">
       如果将最后的
   <code class="parameter">process_sections</code> 参数设为
   <strong><code>TRUE</code></strong>，将得到一个多维数组，包括了配置文件中每一节的名称和设置。<code class="parameter">process_sections</code>
   的默认值是 <strong><code>FALSE</code></strong>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">scanner_mode</code></dt>

     <dd>

      <p class="para">
       Can either be <strong><code>INI_SCANNER_NORMAL</code></strong> (default) or 
       <strong><code>INI_SCANNER_RAW</code></strong>. If <strong><code>INI_SCANNER_RAW</code></strong> 
       is supplied, then option values will not be parsed.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.parse-ini-file-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时以关联数组 <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 返回设置，失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 
 <div class="refsect1 changelog" id="refsect1-function.parse-ini-file-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
         Added optional <code class="parameter">scanner_mode</code> parameter.
         Single quotes may now be used around variable assignments.
         Hash marks (<em>#</em>) may no longer be used as comments
         and will throw a deprecation warning if used.
       </td>
      </tr>

      <tr>
       <td>5.2.7</td>
       <td>
        On syntax error this function will return <strong><code>FALSE</code></strong> rather than an empty
        array.
       </td>
      </tr>

      <tr>
       <td>5.2.4</td>
       <td>
        由数字组成的键名和小节名会被 PHP 当作<a href="language.types.integer.html" class="link">整数</a>来处理，因此以
   0 开头的数字会被当作八进制而以 0x 开头的会被当作十六进制。
       </td>
      </tr>

      <tr>
       <td>5.0.0</td>
       <td>
        该函数也开始处理选项值内的新行。
       </td>
      </tr>

      <tr>
       <td>4.2.1</td>
       <td>
        本函数也开始受到<a href="ini.sect.safe-mode.html#ini.safe-mode" class="link">安全模式</a>和
    <a href="ini.core.html#ini.open-basedir" class="link">open_basedir</a> 的影响。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.parse-ini-file-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2827">
    <p><strong>Example #1 <var class="filename">sample.ini</var> 的内容</strong></p>
    <div class="example-contents">
<div class="cdata"><pre>
; This is a sample configuration file
; Comments start with &#039;;&#039;, as in php.ini

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = &quot;/usr/local/bin&quot;
URL = &quot;http://www.example.com/~username&quot;

[third_section]
phpversion[] = &quot;5.0&quot;
phpversion[] = &quot;5.1&quot;
phpversion[] = &quot;5.2&quot;
phpversion[] = &quot;5.3&quot;
</pre></div>
    </div>

   </div>
   <div class="example" id="example-2828">
    <p><strong>Example #2 <span class="function"><strong>parse_ini_file()</strong></span> 例子</strong></p>
    <div class="example-contents"><p>
      <a href="language.constants.html" class="link">常量</a>也可以在 ini
   文件中被解析，因此如果在运行 <span class="function"><strong>parse_ini_file()</strong></span>
   之前定义了常量作为 ini 的值，将会被集成到结果中去。只有 ini
   的值会被求值。例如：
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">'BIRD'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Dodo&nbsp;bird'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Parse&nbsp;without&nbsp;sections<br /></span><span style="color: #0000BB">$ini_array&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #DD0000">"sample.ini"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Parse&nbsp;with&nbsp;sections<br /></span><span style="color: #0000BB">$ini_array&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #DD0000">"sample.ini"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_array</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [one] =&gt; 1
    [five] =&gt; 5
    [animal] =&gt; Dodo bird
    [path] =&gt; /usr/local/bin
    [URL] =&gt; http://www.example.com/~username
    [phpversion] =&gt; Array
        (
            [0] =&gt; 5.0
            [1] =&gt; 5.1
            [2] =&gt; 5.2
            [3] =&gt; 5.3
        )

)
Array
(
    [first_section] =&gt; Array
        (
            [one] =&gt; 1
            [five] =&gt; 5
            [animal] =&gt; Dodo bird
        )

    [second_section] =&gt; Array
        (
            [path] =&gt; /usr/local/bin
            [URL] =&gt; http://www.example.com/~username
        )

    [third_section] =&gt; Array
        (
            [phpversion] =&gt; Array
                (
                    [0] =&gt; 5.0
                    [1] =&gt; 5.1
                    [2] =&gt; 5.2
                    [3] =&gt; 5.3
                )

        )

)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2829">
    <p><strong>Example #3 <span class="function"><strong>parse_ini_file()</strong></span> parsing a php.ini file</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;A&nbsp;simple&nbsp;function&nbsp;used&nbsp;for&nbsp;comparing&nbsp;the&nbsp;results&nbsp;below<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">yesno</span><span style="color: #007700">(</span><span style="color: #0000BB">$expression</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(</span><span style="color: #0000BB">$expression&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">'Yes'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'No'</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;the&nbsp;path&nbsp;to&nbsp;php.ini&nbsp;using&nbsp;the&nbsp;php_ini_loaded_file()&nbsp;<br />//&nbsp;function&nbsp;available&nbsp;as&nbsp;of&nbsp;PHP&nbsp;5.2.4<br /></span><span style="color: #0000BB">$ini_path&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">php_ini_loaded_file</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Parse&nbsp;php.ini<br /></span><span style="color: #0000BB">$ini&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_path</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Print&nbsp;and&nbsp;compare&nbsp;the&nbsp;values,&nbsp;note&nbsp;that&nbsp;using&nbsp;get_cfg_var()<br />//&nbsp;will&nbsp;give&nbsp;the&nbsp;same&nbsp;results&nbsp;for&nbsp;parsed&nbsp;and&nbsp;loaded&nbsp;here<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'(parsed)&nbsp;magic_quotes_gpc&nbsp;=&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">yesno</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini</span><span style="color: #007700">[</span><span style="color: #DD0000">'magic_quotes_gpc'</span><span style="color: #007700">])&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">'(loaded)&nbsp;magic_quotes_gpc&nbsp;=&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">yesno</span><span style="color: #007700">(</span><span style="color: #0000BB">get_cfg_var</span><span style="color: #007700">(</span><span style="color: #DD0000">'magic_quotes_gpc'</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
(parsed) magic_quotes_gpc = Yes
(loaded) magic_quotes_gpc = Yes
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.parse-ini-file-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    本函数和 <var class="filename">php.ini</var>
    文件没有关系，该文件在运行脚本时就已经处理过了。本函数可以用来读取你自己的应用程序的配置文件。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    如果 ini 文件中的值包含任何非字母数字的字符，需要将其括在双引号中（&quot;）。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    有些保留字不能作为 ini 文件中的键名，包括：null，yes，no，true 和 false。值为
    null，no 和 false 等效于 &quot;&quot;，值为 yes 和 true 等效于 &quot;1&quot;。字符
    <em>{}|&amp;~![()&quot;</em> 也不能用在键名的任何地方，而且这些字符在选项值中有着特殊的意义。
   </span>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.parse-ini-file-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="parse_ini_string.html" class="function" rel="rdfs-seeAlso">parse_ini_string()</a> - 解析配置字符串</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>